package com.isoftstone.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.isoftstone.pojo.Institution;
import com.isoftstone.pojo.User;
import com.isoftstone.service.InstitutionService;

@Controller

public class InstitutionController {

	@Autowired
	private InstitutionService institutionService;

	@RequestMapping("/listinstitution")
	public String list(Model model) {
		List<Institution> list = institutionService.getALLInstitution();
		model.addAttribute("list", list);
		return "listinstitution";
	}

	@RequestMapping("/detailinstitution")
	public String detail(Model model, String id) {
		Institution institution = institutionService.select(Integer.parseInt(id));
		model.addAttribute("institution", institution);
		return "detailinstitution";
	}

	@RequestMapping("/updateinstitution")
	public String update(Model model, String id,Institution institutionone) {
		java.util.Date dt = new java.util.Date();
		java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = sdf.format(dt);
		institutionone.setCreatetime(currentTime);
		institutionService.Update(institutionone);
		Institution institution = institutionService.select(Integer.parseInt(id));
		model.addAttribute("institution", institution);
		return "updateinstitution";
	}

//	@RequestMapping("/updateinstitution")
//	public String update(Institution institution) {
//		int i = institutionService.Update(institution);
//		if (i > 0) {
//			return null;
//		}
//		return "updateinstitution";
//	}
//	
	@RequestMapping(path = "/addinstitution", method = RequestMethod.POST)
	public String register4Post(Model model, Institution institution) {
		if (institution.getName() != null) {
			java.util.Date dt = new java.util.Date();
			java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = sdf.format(dt);
			institution.setCreatetime(currentTime);
			System.out.println(currentTime);
			int i = institutionService.insert(institution);
//			List<Institution> list = institutionService.getALLInstitution();
//			model.addAttribute("list", list);
			if (i > 0) {
				return null;
			}
		}
		return "addinstitution";
	}

	@RequestMapping(path = "/addinstitution", method = RequestMethod.GET)
	public String register4Get(Model model, Institution institution) {
		if (institution.getName() != null) {
			java.util.Date dt = new java.util.Date();
			java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = sdf.format(dt);
			institution.setCreatetime(currentTime);
//			System.out.println(currentTime);
			int i = institutionService.insert(institution);
//		List<Institution> list = institutionService.getALLInstitution();
//		model.addAttribute("list", list);
			if (i > 0) {
				return null;
			}
		}
		return "addinstitution";
	}

	@RequestMapping("/deleteinstitution")
	public String deleteinstitution(Model model,String id) {
		int i = institutionService.cancellation(Integer.parseInt(id));
		List<Institution> list = institutionService.getALLInstitution();
		model.addAttribute("list", list);
		return "listinstitution";
	}
}
